import axios from "axios";
import store from "@/store";
import router from '@/router';
import { message } from "ant-design-vue";
import {LocalStoreageUtil} from '@/utils/LocalStoreage';
const request = axios.create({
    // baseURL: 'http://localhost:8080'
    // baseURL: 'http://1.12.241.181:8080'
    baseURL: process.env.VUE_APP_SERVER
});
request.interceptors.request.use((config:any) => {
    const token = store.state.user.token;
    // console.log('token----axioso',token);
    
    console.log("请求参数：", config);
    // 读取存储在本地的token,将token挂载请求头上，用于后端鉴权
    if(token != null){
        config.headers.token = token;
    }
    return config;
},error => {
    console.log('我是axios中的request请求');
    
    return Promise.reject(error);
});

// 相应拦截器
request.interceptors.response.use(resp => {
    console.log("相应数据：", resp);

    return resp;
},error => {
    // console.log('error+++++',error);
    const response = error.response;
    const status = response.status;
    // router.replace('/login');
    if(status == '401'){
        store.commit('GETUSERINFO', {})
        // 清空本地存储
        LocalStoreageUtil.removeValue('token');
        router.replace('/login'); // 跳转到登录页登录
    }else if (status == '403'){
        message.error("账号不可用，请联系管理员解封");
        router.replace('/login'); // 跳转到登录页登录
    }
    
    return Promise.reject(error);
});

export default request;